package com.douqu.game.main.server;


import com.douqu.game.core.e.E_ProfileVersion;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.msg.AMsgChannel;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;

import javax.swing.*;

/**
 * Created by bean
 * 2017-03-23 16:58.
 */
public class SpringContext
{
    /**
     * spring管理器
     */
    private static ApplicationContext applicationContext;

    public static void init(ApplicationContext ac)
    {
        applicationContext = ac;
    }

    public static <T> T getBean(Class cls)
    {
        return (T) applicationContext.getBean(cls);
    }

    public static <T> T getBean(String name)
    {
        return (T) applicationContext.getBean(name);
    }

    public static TaskExecutor getTaskExecutor()
    {
        return SpringContext.getBean(ConstantFactory.TASK_EXECUTOR_NAME);
    }

    public static AMsgChannel getMessageChannel(Class cls){
        return SpringContext.getBean(cls);
    }
//    public static PlatformSDK getPlatformSDK(Class cls){
//        return SpringContext.getBean(cls);
//    }

    public static void stop()
    {
        SpringApplication.exit(applicationContext);
    }

//    public static String getProfile()
//    {
//        Environment env = getBean(Environment.class);
//        return env.getProperty("profile");
//    }

    public static E_ProfileVersion getVersion()
    {
        Environment env = getBean(Environment.class);
        return E_ProfileVersion.forKey(env.getProperty("profile"));
    }

    public static String getHttpPort()
    {
        Environment env = getBean(Environment.class);
        return env.getProperty("server.port");
    }

    public static void showConfig()
    {
        Environment env = getBean(Environment.class);
        E_ProfileVersion version = getVersion();
        if(version == null)
        {
            JOptionPane.showMessageDialog(null, "版本为空,请检查配置文件 -> " + env.getProperty("profile"));
            return;
        }
        LogUtils.info("VERSION ------------>" + version);
        LogUtils.info("WriteDBUrl---------->" + env.getProperty("spring.datasource.url"));
        LogUtils.info("ReadDBUrl----------->" + env.getProperty("spring.datasource.read.url"));
    }

    public static boolean isDev()
    {
        return getVersion() == E_ProfileVersion.DEV;
    }


    public static boolean isProduct()
    {
        return getVersion() == E_ProfileVersion.PRODUCT;
    }

}
